package spring.cloud.tasks.tasks_assign_manager.task_item_assign;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import spring.cloud.tasks.common.task_item_allot.OnlineExecutor;
import spring.cloud.tasks.common.task_item_allot.TaskItem;
import spring.cloud.tasks.tasks_assign_manager.domain.WaitedAssignTaskInfo;
import spring.cloud.tasks.tasks_assign_manager.service.TaskItemAssignService;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign.pick_up.PickUp;

import java.util.List;

/**
 * 域下重排，移除已经存在所有executor，重新获取executor，重新获取任务shards
 */
@Slf4j
@Setter
@Getter
public class NameSpaceTasksTaskItemAssign extends PickUp {
    public NameSpaceTasksTaskItemAssign(TaskItemAssignService taskItemAssignService) {
        super(taskItemAssignService);
    }

    @Override
    protected void log() {
        log.info("Execute the {} ", this.getClass().getSimpleName());
    }

    @Override
    public boolean pickUp(CuratorFramework curatorFramework,//
                          List<String> taskIdList,//
                          List<String> enableTaskIdList,//
                          List<TaskItem> pickUpEdTaskItemList,//
                          List<TaskItem> discardTaskItemList,//
                          List<OnlineExecutor> onlineExecutorList,//here is use 'protected List<OnlineExecutor> getOnlineExecutorList();'
                          List<OnlineExecutor> ownTrafficOnlineExecutorList,//
                          PreferExecutorSettings preferExecutorSettings) throws Exception {
        // 修正所有executor对所有任务的taskIdList
        for (int j = 0; j < taskIdList.size(); j++) {
            TaskItemAssignUtils.refreshOnlineExecutorSupportedTaskIdList(curatorFramework, onlineExecutorList, taskIdList.get(j));
        }

        // 获取该域下所有enable任务的所有分片
        for (int i = 0; i < enableTaskIdList.size(); i++) {
            //一个任务多个实例
            WaitedAssignTaskInfo waitedAssignTaskInfo = new WaitedAssignTaskInfo();
            pickUpEdTaskItemList.addAll(TaskItemAssignUtils.createTaskItemList(curatorFramework, waitedAssignTaskInfo));
        }

        return true;
    }


}
